package com.lyk.coursearrange.controller;

import com.lyk.coursearrange.common.ServerResponse;
import com.lyk.coursearrange.common.core.base.result.R;
import com.lyk.coursearrange.common.core.domain.AjaxResult;
import com.lyk.coursearrange.common.log.annotation.Log;
import com.lyk.coursearrange.common.log.enums.OperationType;
import com.lyk.coursearrange.dao.OnlineCategoryDao;
import com.lyk.coursearrange.dao.ScheduleDao;
import com.lyk.coursearrange.entity.Admin;
import com.lyk.coursearrange.entity.Schedule;
import com.lyk.coursearrange.entity.dto.ScheduleDto;
import com.lyk.coursearrange.entity.request.UserLoginRequest;
import com.lyk.coursearrange.service.ScheduleService;
import com.lyk.coursearrange.service.StudentService;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lequal
 * @since 2020-03-06
 */
@RestController
@RequestMapping("/schedule")
public class ScheduleController {



    @Autowired
    private ScheduleService  scheduleService;
    @Autowired
    private ScheduleDao scheduleDao;
    @Autowired
    private StudentService studentService;
    @Autowired
    private OnlineCategoryDao onlineCategoryDao;


    /**
     * 查询教师列表
     * @return
     */
    @GetMapping("/teacherlist")
/*    @Log(title = "查询教师列表", operationType = OperationType.SELECT)*/
    public AjaxResult teacherlist() {
            return AjaxResult.success("获取教师列表成功!!!",scheduleService.getteacherlist());
    }


    /**
     * 查询课程列表
     * @return
     */
    @GetMapping("/categorylist")
/*    @Log(title = "查询课程列表", operationType = OperationType.SELECT)*/
    public AjaxResult categorylist() {
        return AjaxResult.success("获取课程列表成功!!!",scheduleService.categorylist());
    }


    /**
     * 查询科目列表
     * @return
     */
    @GetMapping("/categorytwolist")
/*    @Log(title = "查询科目列表", operationType = OperationType.SELECT)*/
    public AjaxResult categorytwolist() {
        return AjaxResult.success("获取科目列表成功!!!",scheduleService.categorytwolist());
    }


    /**
     * 查询学生列表接口
     */
    @GetMapping("/studentlist/{teacherid}")
  /*  @Log(title = "查询学生列表接口", operationType = OperationType.SELECT)*/
    public AjaxResult studentlist(@PathVariable Long teacherid) {
        return AjaxResult.success("获取学生列表成功!!!",studentService.studentlist(teacherid));
    }



    /**
     * 根据条件查询排课信息
     * @return
     */
    @PostMapping("/selectschedule")
    @Log(title = "分页查询排课列表", operationType = OperationType.SELECT)
    public AjaxResult selectschedule(@RequestBody Map<String,String> param) {
        List<Schedule> list = scheduleService.selectschedule(param);
        List<ScheduleDto> scheduleDtoList = new ArrayList<>();
        list.forEach(schedule -> {
            scheduleDtoList.add(ScheduleDto.builder()
                            .scheduleId(schedule.getScheduleId())
                            .courseId(schedule.getCourseId())
                            .courseName(onlineCategoryDao.selectname(schedule.getCourseId()))
                            .courseDate(schedule.getCourseDate())
                            .courseTimeOn(schedule.getCourseTimeOn())
                            .courseEndTime(schedule.getCourseEndTime())
                            .build());
        });
        return AjaxResult.success("排课信息获取成功!!!",scheduleDtoList);
    }
    /**
     * 单个查询排课信息
     */
    @PostMapping("/selectscheduleone")
    @Log(title = "单个查询排课信息", operationType = OperationType.SELECT)
    public R selectscheduleone(@RequestBody Map<String,String> param) {
        Long schedule_id = Long.valueOf(param.get("scheduleId"));
        String realname = param.get("realname");
        System.out.println(schedule_id);
        ScheduleDto schedule = new ScheduleDto();
        Schedule schedule1  =  scheduleService.selectscheduleone(schedule_id);
        BeanUtils.copyProperties(schedule1,schedule);
        schedule.setCourseName(onlineCategoryDao.selectname(schedule1.getCourseId()));
        schedule.setStudentList(scheduleDao.selectliststudentid(schedule_id,realname));
        schedule.setTeacherName(scheduleDao.selectteachername(schedule1.getTeacherId()));
        return R.ok()
                .data("list",schedule)
                .message("单个查询排课信息成功！！！");
    }










    /**
     * 添加排课信息
     * @return
     */
    @PostMapping("/addschedule")
    @Log(title = "添加排课信息", operationType = OperationType.INSERT)
    public AjaxResult addschedule(@RequestBody Map<String,String> param) {
        if(scheduleService.addschedule(param)){
            return AjaxResult.success("添加排课信息成功!!!");
        }else {
            return AjaxResult.error("添加排课信息失败!!!");
        }

    }


    /**
     * 修改排课信息
     */
    @PutMapping("updateschedule")
    @Log(title = "修改排课信息", operationType = OperationType.UPDATE)
    public AjaxResult updateschedule(@RequestBody Map<String,String> param) {
        if(scheduleService.updateschedule(param)){
            return AjaxResult.success("修改排课信息成功!!!");
        }else {
            return AjaxResult.error("修改排课信息失败!!!");
        }

    }


    /**
     * 签到
     */
    @PutMapping("signin")
    @Log(title = "签到", operationType = OperationType.OTHER)
    public R signin(@RequestBody Map<String,String> param) {
        if(scheduleService.signin(param)){
            return R.ok().message("签到成功!!!");
        }else {
            return R.error().message("签到失败!!!");
        }

    }

    /**
     * 结课
     */
    @PutMapping("classending")
    @Log(title = "结课", operationType = OperationType.OTHER)
    public R classending(@RequestBody Map<String,String> param) {
        if(scheduleService.classending(param)){
            return R.ok().message("结课成功!!!");
        }else {
            return R.error().message("结课失败!!!");
        }

    }























}
